$(function () {
    // 表单校验
    //使用表单校验插件
    $('.form-login').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            username: {
                validators: {
                    notEmpty: {
                        message: '用户名不能为空'
                    },
                    stringLength: {
                        min: 2,
                        max: 6,
                        message: '用户名长度必须在2到6之间'
                    },
                    callback:{
                        message:'用户名错误'
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: '密码不能为空'
                    },
                    stringLength: {
                        min: 4,
                        max: 12,
                        message: '用户名长度必须在4到12之间'
                    },
                    callback:{
                        message:'密码错误'
                    }
                }
            },
        }
    });
    // 重置按钮
    $('.btn-reset').click(function () {
        var validator = $(".form-login").data('bootstrapValidator');
        validator.resetForm();
    })
    // 注册表单校验成功事件
    $('.form-login').on('success.form.bv', function (e) {
        e.preventDefault();
        $.ajax({
            type: "post",
            url: "/employee/employeeLogin",
            data: $('.form-login').serialize(),
            dataType: "json",
            success: function (res) {
                console.log(res);
                if(res.success) {
                    location.href = "./index.html";
                }
                if(res.error == 1000) {
                    $('.form-login').data('bootstrapValidator').updateStatus('username', 'INVALID', 'callback');
                }
                if(res.error == 1001) {
                    $('.form-login').data('bootstrapValidator').updateStatus('password', 'INVALID', 'callback');
                }
            }
        });
    })
})